argss <- commandArgs(trailingOnly = TRUE)

print(argss)

dat <- argss[2]
name = argss[3]

rc=read.table(dat, col.names="read_count", stringsAsFactors=F)
print(head(rc))
len=length(rc$read_count)

# names=names[order(names$win),]
# names$legend=strsplit(names$file, '[.]')[[1]][2] #[.]


# rc92=read.table('92.rc.txt')
# rc91=read.table('CHG002591/1.sort.200000.sample.txt')
# rc93=sort(rc93)
# len=length(rc93)
# x=seq(from=0, to=1, length.out=len)

# rc93=rc93[length(which(rc93 == 0),]
# rc93=rc93[length(which(rc93 == 0):len),]
# rc93=rc93[length(which(rc93 == 0)):len,]
# len=length(rc93)

# len0=length(which(rc93 == 0))

# rc93=rc93[len0:len,]

# len=len-len0+1


# for (i in 1:100) {
	# rc93$V3[i]=sum(rc93[1:i])
	# rc93$V3= rc93$V3 / rc93$V3[100]
# }

# rc93$V4= rc93$V3 / rc93$V3[100]
# x=seq(0.01, 1, 0.01)

prob=.999
xlim=c(0, len)
ylim=c(0, quantile(rc$read_count, probs = prob, name=F))
# ylim=xlim
# hlineby=.1
# hline=seq((ylim[1]+hlineby), (ylim[2]-hlineby), hlineby)
# vline=hline


pdf("1.rc_hist.pdf", width=12, height=4)

par(ann=F, xaxs='i', yaxs='i')

plot(rc$read_count, xlim=xlim, ylim=ylim, cex=.5, pch=20, col="blue")

# abline(0,1, col='black')

# abline(v=vline, h=hline, lty=3, col='gray')

title_main=paste("Genome wide read distribution for sample ", name)

title(main=title_main, xlab="Window index", ylab="Read count")

# names$color=rainbow(nrow(names))

# max_num=10000

# for (i in 1:nrow(names)) {
	# rc0=read.table(names[i,1])
	# rc=sample(rc0, )
	# rc=sort(rc)
	# len=length(rc)
	# sum=sum(rc)
	# for (i in 1:len) {
		# rc$V3[i]=sum(rc[1:i]) / sum
	# }
	# x=seq(from=0, to=1, length.out=len)
	# lines(x, rc$V3, col=$names$color[i])
	
	# names$legend[i]=strsplit(names$file[i], '[.]')[[1]][2]

# }

# simu=runif(max_num)
# num_sub=round(max_num/10)
# count=hist(simu, breaks=num_sub, plot=F)$counts
# rc=sort(count)
# rc=c(0, rc)
# num_sub=num_sub+1 # start from (0,0)
# x=seq(from=0, to=1, length.out=num_sub)
# sum=sum(rc)
# rc_norm=NULL
# for (ii in 1:num_sub) {
	# rc_norm[ii]=sum(rc[1:ii]) / sum
# }

# gini_simu=1-((2*sum(rc_norm)-rc_norm[1]-rc_norm[num_sub])/(num_sub-1))
# gini_simu_str=paste('simu', gini_simu, sep=' ')

# lines(x, rc_norm, col='gray')

# gini_str=vector('character')

# for (i in 1:nrow(names)) {
	# rc0=read.table(names[i,1])
	# if (max_num < nrow(rc0)) {
		# rc=sample(rc0$V1, max_num)
		# len=max_num
	# }else {
		# rc=rc0$V1
		# len=length(rc)
	# }
	
	# rc=sort(rc)
	
	# sum=sum(rc)
	# rc_norm=NULL
	# for (ii in 1:len) {
		# rc_norm[ii]=sum(rc[1:ii]) / sum
	# }
	# x=seq(from=0, to=1, length.out=len)
	# lines(x, rc_norm, col=names$color[i])
	
	# gini=1-((2*sum(rc_norm)-rc_norm[1]-rc_norm[len])/(len-1))
	# gini_str[i]=paste(names$legend[i], gini, sep=' ')
	
# }

# legend("topleft", inset=.05, lty=c(1,1,1), c(gini_str, gini_simu_str), col=c(names$color, 'gray'))

# legend("topleft", inset=.05, lty=c(1,1,1), c(names$legend, 'simu'), col=c(names$color, 'gray'))

dev.off()



prob=.999
breakss=200

# xlim=c(0, len)

rc1=rc$read_count[which(rc$read_count < quantile(rc$read_count, probs=prob))]

# hist0=hist(rc$read_count, breaks=round(len/10), plot=F)
hist0=hist(rc1, breaks=breakss, plot=F)
ylim=c(0, quantile(hist0$counts, probs = prob, name=F))

pdf("1.rc_freq.pdf", width=8, height=6)

# par(ann=F, yaxs='i')
title_main=paste("Frequency of read count for sample ", name)

# hist(rc$read_count, breaks=round(len/10), ylim=ylim, cex=.5, border='blue', xlab ="Read count", main=title_main)

hist(rc1, breaks=breakss, ylim=ylim, cex=.5, border='blue', xlab ="Read count", main=title_main)

dev.off()

save.image()